/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package presenter;

/**
 *
 * @author Al
 */
public class Node<E> {
    Node next;
    Node previous;
    E object;
    
    public Node() {
        
    }
    
    public Node(E object) {
        this();
        this.object=object;
    }
    
    public void setNext(Node next) {
        this.next=next;
    }
    
    public void setPrevious(Node previous) {
        this.previous=previous;
    }
    
    public Node getNext() {
        return next;
    }
    
    public Node getPrevious() {
        return previous;
    }
    
    public void createCircularList(E[] objects) {
        Node p=this;
        object=objects[0];
        for(int i=1; i<objects.length-1; i++) {
            Node<E> next=new Node<E>(objects[i]);
            
            next.setPrevious(p);
            p.setNext(next);
            p=p.next;
            
        }
        Node<E> last=new Node<E>(objects[objects.length-1]);
        last.setPrevious(p);
        p.setNext(last);
        
        last.setNext(this);
        previous=last;
        p=this;
        do {
            System.out.println(p.getObject());
            System.out.println(p.getPrevious().getObject());
            p=p.previous;
        } while(p!=this);
    }
    
    public E getObject() {
        return object;
    }
    
    public Node findNode(E object) {
        Node p=this;
        do {
            p=p.getNext();
        } while(p.getObject()!=object&&p!=this);
        
        return p;
    }
}
